import { isNumber } from 'lodash-es';

const USD = {
  format(amount: number | string): string {
    const formatter = new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
      maximumFractionDigits: 0,
    });

    if (isNumber(amount)) {
      return formatter.format(amount).replace(/\$/g, '');
    }

    const f = parseFloat(amount);
    if (isNaN(f)) {
      return amount;
    }

    return formatter.format(f).replace(/\$/g, '');
  },

  parse(value: string): string {
    return value.replace(/,/g, '');
  },
};

export { USD };
